package com.foxchen.fanjingmontainticket.controller.rest;

import com.foxchen.fanjingmontainticket.bean.TicketVO;
import com.foxchen.fanjingmontainticket.domain.TicketRecord;
import com.foxchen.fanjingmontainticket.schedule.DynamicTaskService;
import com.foxchen.fanjingmontainticket.schedule.task.BuyTicketTask;
import com.foxchen.fanjingmontainticket.service.TicketBasicService;
import com.foxchen.fanjingmontainticket.service.TicketRecordService;
import jakarta.annotation.Resource;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.crypto.digest.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.Date;
import java.util.UUID;

import static com.foxchen.fanjingmontainticket.schedule.task.BuyTicketTask.OBJECT_MAPPER;

@RequestMapping("/ticket")
@RestController
public class TaskController {
    @Resource
    private TicketRecordService ticketRecordService;
    @Resource
    private DynamicTaskService dynamicTaskService;
    @Resource
    private TicketBasicService ticketBasicService;
    @Resource
    private JavaMailSender mailSender;

    @PostMapping("/list-time-range-by-date")
    public String listTicketTime(@RequestBody TicketVO ticket) {
        return ticketBasicService.listAllTimeByDate(ticket);
    }

    @PostMapping("/list-user")
    public String listAllUser(@RequestBody TicketVO ticket) {
        return ticketBasicService.listPerson(ticket);
    }


    @PostMapping("/submit")
    public String submit(@RequestBody TicketVO ticket) throws Exception{
        // 校验是否已经创建过
        String jsonData = OBJECT_MAPPER.writeValueAsString(ticket);
        String jsonMd5 = MD5.of().digestHex(jsonData);
        boolean exists = ticketRecordService.lambdaQuery().eq(TicketRecord::getFromJsonMd5, jsonMd5).exists();
        if(exists){
            return "相同任务请勿重复提交.";
        }

        // 保存记录，初始化状态


        Instant instant = ticket.getAppStartBuyTime().toInstant();
        for (String leaveTime : ticket.getTimeList()) {
            String[] split = leaveTime.split("-");
            // 任务基本信息
            TicketVO vo = new TicketVO();
            BeanUtils.copyProperties(ticket, vo);
            vo.setTimeRangeId(split[0]);
            vo.setUseTimeBeg(split[1]);
            vo.setUseTimeEnd(split[2]);

            // 任务数据 先保存默认状态0
            TicketRecord ticketRecord = new TicketRecord();
            ticketRecord.setUser(ticket.getUser());
            ticketRecord.setStatus(0);
            ticketRecord.setCreateTime(DateUtil.formatDateTime(new Date()));
            ticketRecord.setFromJsonMd5(jsonMd5);
            ticketRecord.setTaskUuid(UUID.randomUUID().toString());
            ticketRecord.setUserJson(OBJECT_MAPPER.writeValueAsString(ticket.getLeaveUser()));
            ticketRecord.setTimeDetail(leaveTime);
            ticketRecord.setProdId(ticket.getProdId());
            ticketRecord.setTaskStartTime(DateUtil.formatDateTime(ticket.getAppStartBuyTime()));
            ticketRecordService.save(ticketRecord);
            dynamicTaskService.addTask(new BuyTicketTask(ticketRecordService,vo,ticketRecord,mailSender),instant);
        }

        return "success";
    }
}
